package com.zhangtai.modules.service.impl.employee;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.controller.admin.JobController;
import com.zhangtai.modules.controller.aouthLogin.LoginController;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dao.EmployeeOverworkDao;
import com.zhangtai.modules.dao.SubEmployeeOverworkDao;
import com.zhangtai.modules.dto.*;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.employee.EmployeeOverworkEntity;
import com.zhangtai.modules.entity.employee.SubEmployeeOverworkEntity;
import com.zhangtai.modules.service.employee.EmployeeInfoService;
import com.zhangtai.modules.service.employee.EmployeeOverworkService;
import com.zhangtai.modules.service.employee.SubEmployeeOverworkService;
import com.zhangtai.utils.BeanUtilsMabach;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import com.zhangtai.utils.SnowflakeIdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
public class EmployeeOverworkServiceImpl extends ServiceImpl<EmployeeOverworkDao, EmployeeOverworkEntity> implements EmployeeOverworkService {
    @Autowired
    private EmployeeOverworkDao employeeOverworkDao;

    @Autowired
    private SubEmployeeOverworkService subEmployeeOverworkService;

    @Autowired
    private SubEmployeeOverworkDao subEmployeeOverworkDao;

    @Autowired
    private JobController jobController;

    @Autowired
    private SnowflakeIdWorker snowflake;
    @Autowired
    private MemberLoginController memberLoginController;
    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Autowired
    private LoginController loginController;

    @Override
    public PageResult queryPage(@RequestParam(value = "oprationName", required = false) String oprationName,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        Long oid = data.getLong("id"); //只能查询自己的信息
        Page<EmployeeOverworkPageDto> page = new Page(pageNum,pageSize);
        List<EmployeeOverworkEntity> overWorkList = employeeOverworkDao.getOverWorkOne(oprationName, page,oid);
        ArrayList<EmployeeOverworkPageDto> dtos = new ArrayList<EmployeeOverworkPageDto>();
        for (EmployeeOverworkEntity index: overWorkList
             ) {
            EmployeeOverworkPageDto employeeOverworkPageDto = BeanUtilsMabach.doToDto(index, EmployeeOverworkPageDto.class);
            List<SubEmployeeOverworkEntity> subOverWorkListVo = subEmployeeOverworkDao.getSubOverWorkListVo(index.getId().toString());
            if(!CollectionUtils.isEmpty(subOverWorkListVo)) {
                employeeOverworkPageDto.setSubEmployeeOverworkEntities(subOverWorkListVo);
                dtos.add(employeeOverworkPageDto);
            }
        }

        page.setRecords(dtos);


        return new PageResult(page);
    }

    @Override
    public PageResult queryPageHr(@RequestParam(value = "oprationName", required = false) String oprationName,
                                  @RequestParam(value = "corpName", required = false) String corpName,
                                  @RequestParam(value = "orgNname", required = false) String orgNname,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , HttpServletRequest request) throws Exception {
        R uR = loginController.getUserInfo(request);
        JSONObject user = (JSONObject) uR.getData();
        //获取当前用户角色下公司Id
        R checkR = loginController.checkOrgPermission(request,null,false);

        List<Long> orgIds = (List<Long>) checkR.getData();

        Page<EmployeeOverworkPageDto> page = new Page(pageNum,pageSize);

        List<EmployeeOverworkEntity> overWorkList = employeeOverworkDao.getOverWorkOneHr(oprationName,corpName,orgNname, page,orgIds);
        ArrayList<EmployeeOverworkPageDto> dtos = new ArrayList<EmployeeOverworkPageDto>();
        for (EmployeeOverworkEntity index: overWorkList
                ) {
            EmployeeOverworkPageDto employeeOverworkPageDto = BeanUtilsMabach.doToDto(index, EmployeeOverworkPageDto.class);
            List<SubEmployeeOverworkEntity> subOverWorkListVo = subEmployeeOverworkDao.getSubOverWorkListVo(index.getId().toString());
            if(!CollectionUtils.isEmpty(subOverWorkListVo)) {
                employeeOverworkPageDto.setSubEmployeeOverworkEntities(subOverWorkListVo);
                dtos.add(employeeOverworkPageDto);
            }
        }

        page.setRecords(dtos);


        return new PageResult(page);
    }

    @Override
    public PageResult queryRecordPage(@RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            ,HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        Long oid = data.getLong("id"); //只能查询自己的信息
        Page<OverworkRecordDto> page = new Page(pageNum,pageSize);
        String code=data.getString("usercode");
        List<OverworkRecordDto> recordOverWorkList = employeeOverworkDao.getRecordOverWorkList(name, page,code);
//        employeeOverworkDao.getRecordOverWorkList(name, page);
        for (OverworkRecordDto item:recordOverWorkList
             ) {
            item.setStatus(item.getSatus());
        }

        page.setRecords(recordOverWorkList);


        return new PageResult(page);
    }

    @Override
    public EmployeeOverworkDto getByIdE(Long id) {
        EmployeeOverworkEntity employeeOverworkEntity = employeeOverworkDao.getOverWorkListById(id);
        EmployeeOverworkDto overWorkDto = BeanUtilsMabach.doToDto(employeeOverworkEntity, EmployeeOverworkDto.class);

        List<SubEmployeeOverworkEntity> subOverWorkListVo = subEmployeeOverworkDao.getSubOverWorkListVo(id.toString());

        if(!CollectionUtils.isEmpty(subOverWorkListVo)) {
            overWorkDto.setSubEmployeeOverworkEntityList(subOverWorkListVo);
        }

        return overWorkDto;
    }

    @Override
    public EmployeeOverworkEntity getById(Long id) {
        EmployeeOverworkEntity one = this.getOne(new QueryWrapper<EmployeeOverworkEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(EmployeeOverworkEntity entity) {
        entity.setIsDelete(0);
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    @Transactional
    public R save(EmployeeOverworkDto employeeOverworkDto,HttpServletRequest request) throws Exception {
        EmployeeOverworkEntity overWorkListById=null;

        JSONObject data = memberLoginController.getUserInfo(request).getData();
        Long oid = data.getLong("id"); //只能查询自己的信息
        String code=data.getString("usercode");
        Long orgId=data.getLong("orgId");
        Long depId=data.getLong("depId");
        Long jobId=data.getLong("jobId");
        if(employeeOverworkDto
                .getId()!=null) {
            overWorkListById = employeeOverworkDao.getOverWorkListById(employeeOverworkDto.getId());
        }
        //查不出来就新增 否则修改
        if(!ObjectUtils.isEmpty(overWorkListById)) {
            //删除再新增
//            if()
            removeOneById(employeeOverworkDto.getId());

        }

        EmployeeOverworkEntity overworkEntity = BeanUtilsMabach.doToDto(employeeOverworkDto
                , EmployeeOverworkEntity.class);
        //id
        long nextId = snowflake.nextId();
        overworkEntity.setCreateTime(new Date());
        overworkEntity.setId(nextId);
        //公司
        overworkEntity.setCorpId(orgId);
        //部门
        overworkEntity.setOrgId(depId);
        //岗位
        overworkEntity.setJobId(jobId);
        //oid
        overworkEntity.setOid(oid);
        if(overworkEntity.getSatus()!=null){

        }else {
            overworkEntity.setSatus(0);
        }
        saveE(overworkEntity);

        List<SubEmployeeOverworkEntity> subList = new ArrayList<SubEmployeeOverworkEntity>();
        if (!CollectionUtils.isEmpty(employeeOverworkDto.getSubEmployeeOverworkEntityList())) {
            subList = employeeOverworkDto.getSubEmployeeOverworkEntityList();
            for (SubEmployeeOverworkEntity item : subList
                    ) {
                item.setTeoId(nextId);
                item.setIsDelete(0);
                subEmployeeOverworkService.saveE(item);

            }
        }
        return R.ok();
    }

    @Override
    public R updateByIdE(EmployeeOverworkEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }


    @Override
    public R deletByIdE(Long id) {
        EmployeeOverworkEntity entity = new EmployeeOverworkEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }

        List<SubEmployeeOverworkEntity> subOverWorkListVo = subEmployeeOverworkDao.getSubOverWorkListVo(id.toString());
        for (SubEmployeeOverworkEntity item:subOverWorkListVo
             ) {
            item.setIsDelete(1);
            subEmployeeOverworkDao.updateById(item);
            
        }


        return R.ok();
    }

    @Override
    @Transactional
    public R removeOneById(Long id) {
        R r = new R();
        EmployeeOverworkEntity entity = new EmployeeOverworkEntity();
        entity.setId(id);
         employeeOverworkDao.deleteById(id);

        //删除 子表
        List<Long> ids = subEmployeeOverworkDao.getSubOverWorkList(id.toString());
        if(!CollectionUtils.isEmpty(ids)) {
            subEmployeeOverworkDao.deleteBatchIds(ids);
        }


        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}
